package cn.kgc.springboot.controller;

import cn.kgc.springboot.dto.RolePageDTO;
import cn.kgc.springboot.entity.Role;
import cn.kgc.springboot.service.RoleService;
import cn.kgc.springboot.util.CodeMsg;
import cn.kgc.springboot.util.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: hengge
 * @version: v1.0
 * @DATE: 2022/11/22
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    //1.查询
    @RequestMapping("all")
    public ResponseResult releList(){
        List<Role> list = roleService.list();  //查询所有的角色信息
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode()); //查询成功
        responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        responseResult.setData(list);

        return responseResult;
    }

    //2.分页
    @RequestMapping("page")
    public ResponseResult pageList(RolePageDTO rolePageDTO){
        ResponseResult responseResult = roleService.selectPage(rolePageDTO);
        return responseResult;
    }

    //3.添加、编辑操作-->使用了saveOrUpdate方法，完成添加和编辑的操作
    @RequestMapping("saveOrUpdate")
    public ResponseResult addRole(Role role){

        ResponseResult responseResult = new ResponseResult();
        //(1)验证角色名(role_name)是否已经存在
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_name", role.getRoleName());
        Role rl = roleService.getOne(roleQueryWrapper);
        Integer roleId = role.getRoleId();
        System.out.println("rl = " + rl);
        System.out.println("roleId = " + roleId);
        if(rl==null || roleId!=null){  //角色名不存在
            //添加完成后自动封装自增主键
            /*它只传入一个实体对象，当你的实体中的主键为null时，他就会执行insert操作，
             *当你的主键不为空时，它就会执行updata操作*/
            roleService.saveOrUpdate(role);
            if(roleId==null){  //主键不存在，执行insert操作
                responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
                responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
            }else{            //主键存在，执行update操作
                responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
                responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
            }
        }else{   //用户名已存在
            responseResult.setCode(CodeMsg.ROLE_EXIST.getCode());
            responseResult.setMsg(CodeMsg.ROLE_EXIST.getMsg());
        }

        return responseResult;
    }

    //4.根据角色id，查询角色详细信息
    @RequestMapping("selectById")
    public ResponseResult selectById(Integer id){

        ResponseResult responseResult = new ResponseResult();
        //根据id查询
        Role role = roleService.getById(id);
        responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
        responseResult.setData(role);

        return responseResult;
    }

    //5.删除操作
    @RequestMapping("deleteById")
    public ResponseResult deleteById(String ids){
        System.out.println("ids = " + ids);//获取前端勾选的id,例如勾选1和2: ids = 1,2
        //删除勾选的ID
        ResponseResult responseResult =  roleService.deleteByIds(ids);

        return responseResult;
    }

    //6.回显: selectPermissionByRoleId,根据角色id(role_id)，查询权限id(per_id)，用于权限数据默认选中
    @RequestMapping("selectPermissionByRoleId")
    public ResponseResult selectPermissionByRoleId(Integer roleId){
        ResponseResult responseResult = roleService.selectPermissionByRoleId(roleId);
        return responseResult;
    }

    //7.添加权限,为用户添加新的权限(per_id)
    @RequestMapping("addRolePermission")
    public ResponseResult addRolePermission(Integer roleId,String permissionIds){
        //从前端获取数据(例如：选中user用户，该用户role_id=2，权限id为17,18，则打印一下查看如下)
        System.out.println("roleId = " + roleId);  //roleId=2
        System.out.println("permissionIds = " + permissionIds); //permissionIds = 17,18

        ResponseResult responseResult =  roleService.addRolePermission(roleId,permissionIds);
        return responseResult;
    }

}
